package com.kitkat.daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.kitkat.dao.UserDAO;
import com.kitkat.entities.User;

@Component
public class UserDAOImpl implements UserDAO{
	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	@Override
	public void saveUser(User user) {
		this.hibernateTemplate.save(user);
	}

	@Override
	public User getById(String id) {
		User user = this.hibernateTemplate.load(User.class, id);
		return user;
	}

	@Override
	public User getByEmail(String email) {
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		@SuppressWarnings("unchecked")
		List<User> userList = session.getNamedQuery("getUserByEmail")
									.setParameter("email", email)
									.list();
		if(userList!=null&&userList.size()>0){
			return userList.get(0);
		}
		return null;
	}

	@Override
	public void updateUser(User user) {
		this.hibernateTemplate.update(user);
	}

}
